/*
 * Copyright (c) 2017. 深圳掌控网络有限公司. All rights reserved.
 */

package com.yiqi.lottery.feature.lottery.presenter;

import android.text.TextUtils;
import com.suozhang.framework.entity.bo.UserAccount;
import com.suozhang.framework.entity.bo.UserInfoBo;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.utils.ValidatorUtil;
import com.suozhang.framework.utils.logger.Logger;
import com.yiqi.lottery.entity.bo.LotteryHistoryBo;
import com.yiqi.lottery.entity.bo.LuckyHistoryBo;
import com.yiqi.lottery.entity.bo.OrderBo;
import com.yiqi.lottery.entity.bo.RegisterBo;
import com.yiqi.lottery.feature.login.contract.LoginContract;
import com.yiqi.lottery.feature.login.dependencies.login.DaggerLoginPresenterComponent;
import com.yiqi.lottery.feature.lottery.contract.LottoContract;
import com.yiqi.lottery.feature.lottery.dependencies.lotto.DaggerLottoPresenterComponent;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import javax.inject.Inject;
import java.util.List;

/**
 *      LIJUWEN
 * @email yiyayiyayaoljw@gmail.com
 * @date 2017/4/11 9:31
 */
public class LottoPresenter implements LottoContract.Presenter {

    private LottoContract.View mView;

    @Inject

    LottoContract.Model mModel;


    public LottoPresenter(LottoContract.View mView) {
        this.mView = mView;
        DaggerLottoPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }


    @Override
    public void getCurrentQi() {
        mModel.getCurrentQi().compose(mView.bindToLife()).subscribe(new Observer<LuckyHistoryBo>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(LuckyHistoryBo luckyHistoryBo) {

                mView.showCurrentSuccess(luckyHistoryBo);
            }

            @Override
            public void onError(Throwable e) {

                mView.showCurrentError(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });

    }

    @Override
    public void doCreateOrder(int type, int multiple, String bet_code, String issue,int is_charsing,int charsing_num,int mut_num,int order_amount,String min_reward,String max_reward) {
        if (multiple < 1) {
            mView.showCreateOrderError("投注倍数不能小于1！");
            return;
        }
        if (TextUtils.isEmpty(bet_code)) {
            mView.showCreateOrderError("投注信息不能为空！");
            return;
        }
        if (TextUtils.isEmpty(issue)) {
            mView.showCreateOrderError("投注期号不能为空！");
            return;
        }
        OrderBo orderBo = new OrderBo(type, multiple, bet_code, issue,2, is_charsing,charsing_num,mut_num,order_amount,min_reward,max_reward);
        mModel.createOrder(orderBo).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.showCreateOrderSuccess("创建订单成功！");
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showCreateOrderError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getLotteryHistory(String label) {
        if (TextUtils.isEmpty(label)) {
            return;
        }
        mModel.getLotteryHistory(label).compose(mView.bindToLife()).subscribe(new Observer<List<LotteryHistoryBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(List<LotteryHistoryBo> resultBos) {
                mView.dismissLoading();
                mView.showLotteryHistorySuccess(resultBos);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showLotteryHistoryError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }
}
